/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unirio.pf.controle;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ricardo
 */
public abstract class BaseServlet extends HttpServlet {
    
    protected abstract void antesPesquisar(HttpServletRequest request,HttpServletResponse response) throws Exception;
    
    protected abstract void pesquisar(HttpServletRequest request,HttpServletResponse response) throws Exception;
    
    protected abstract void detalharImovel(HttpServletRequest request,HttpServletResponse response) throws Exception;
    
    protected abstract void antesSubmeterProposta(HttpServletRequest request,HttpServletResponse response) throws Exception;
    
    protected abstract void submeterProposta(HttpServletRequest request,HttpServletResponse response) throws Exception;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        invocarMetodo(request, response);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        invocarMetodo(request, response);
    }

    private void invocarMetodo(HttpServletRequest request,HttpServletResponse response) {
        String comando = request.getParameter("comando");
	System.out.println("BaseServlet " + comando);
        
        try {
            
            Class partypes[] = new Class[2];
            partypes[0] = HttpServletRequest.class;
            partypes[1] = HttpServletResponse.class;

            Class cls = this.getClass();
            Method meth;
        
            meth = cls.getDeclaredMethod(comando, partypes);

            Object arglist[] = new Object[2];
            arglist[0] = request;
            arglist[1] = response;
        
            Object retobj = meth.invoke(this, arglist);
        
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(BaseServlet.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
}
